﻿using System;
using Common.Tools;
using CommonDao;
using Domain;

namespace CommonService
{
    /// <summary>
    /// 系统配置服务
    /// </summary>
    public class SystemConfigService
    {
        /// <summary>
        /// 获取配置值，根据键
        /// </summary>
        /// <param name="key"></param>
        /// <param name="isCache">是否从缓存中取</param>
        /// <returns></returns>
        public static string GetValue(string key, bool isCache = false)
        {
            try
            {
                if (isCache)
                {
                    var ret = CacheHelper.GetCache(key) as string;
                    if (string.IsNullOrWhiteSpace(ret))
                    {
                        ret = SystemConfigDao.Get(key);
                        CacheHelper.SetCache(key, ret);
                    }
                    return ret;
                }
                return SystemConfigDao.Get(key);
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("获取配置值，根据键出错", ex);
                return "";
            }

        }

        /// <summary>
        /// 设置配置值，根据键
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="isCache">是否加入缓存</param>
        /// <returns></returns>
        public static bool SetValue(string key, string value, bool isCache = false)
        {
            try
            {
                SystemConfigDao.Update(key, value);
                if (isCache)
                    CacheHelper.SetCache(key, value);
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("获取配置值，根据键出错", ex);
                return false;
            }

        }
       
    }
}
